package com.darren.euc.util.haqi;

import com.alibaba.fastjson.JSON;
import com.darren.euc.util.Constant;
import com.darren.euc.util.PropertyConfigurer;
import com.darren.euc.util.haqi.request.*;
import com.darren.euc.util.sms.HttpUtils;
import org.apache.log4j.Logger;

import java.util.Map;


/**
 * Created by Machenike-Pc on 2018/4/2.
 */
public class HaquAPIHandler {
    private static final Logger log = Logger.getLogger(HaquAPIHandler.class);

    private final static String host = PropertyConfigurer.getPropertiesStr("haqi.url");
    private final static String ADD_PACE="/face/user/add";

    private final static  String GET_DEVICE_LIST="/device/app/find";

    private final static String GET_FACE="/face/user/get";

    private final static String RECOGNITION_HISTORY="/recognition/find";

    private final static String WORK_FIND = "/work/app/find";

    private final static String UPDATE_FACE = "/face/user/update";

    private static String connectHaqu(String url,HaquRequest request, String method){
        if(Constant.GET.equals(method)){
            log.info("[testhaqi]:request get:"+request.toString());
            String response = HttpUtils.sendGet(url+"?"+request.toString());
            log.info("[testhaqi]:response get:"+response);
            return  response;
        }
        log.info("[testhaqi]:request post:"+request.toString());
        Map<String,String> values = (Map<String, String>) JSON.parse(request.toString());
        String response = HttpUtils.post(url,values);
        log.info("[testhaqi]:response post:"+response);
        return response;
    }

    /**
     * 添加用户人脸信息
      * @param request
     * @return
     */
    public static String addFace( AddFaceRequest request){
        String url = host+ADD_PACE;
        //Map<String,String> files = new HashMap<>();
        //files.put(new File("d:/666.png").getName(),new File("d:/666.png").getAbsolutePath());
        //return HttpUtils.post(url,values,files);
        return connectHaqu(url,request,Constant.POST);
        // connectHaqu(url,request,Constant.POST);
    }

    public static String updateFace(AddFaceRequest request){
        String url = host+UPDATE_FACE;
        return connectHaqu(url,request,Constant.POST);
    }
    /**
     * 获取人脸信息
      * @param request
     * @return
     */
    public static String getFace(GetFaceRequest request){
        String url = host+GET_FACE;
        return connectHaqu(url,request,Constant.GET);
    }

    /**
     * 获取设备列表
      * @param request
     * @return
     */
    public static String getGetDeviceList(  GetDeviceListRequest request){
        String url = host+GET_DEVICE_LIST;
        return connectHaqu(url,request,Constant.GET);
    }

    /**
     * 获取识别历史记录
      * @param request
     * @return
     */
    public static String getRecognitionHistory( RecognitionHistoryRequest request){
        String url = host+RECOGNITION_HISTORY;
        return connectHaqu(url,request,Constant.GET);
    }

    /**
     * 获取考勤记录
      * @param request
     * @return
     */
    public static String getWorkFind( WorkFindRequest request){
        String url = host+WORK_FIND;
        return connectHaqu(url,request,Constant.GET);
    }

    /**
     * 实时获取考勤记录
     * @param request
     * @return
     */
    public static String getWorkFind( WorkFindOntimeRequest request){
        String url = host+WORK_FIND;
        return connectHaqu(url,request,Constant.GET);
    }
}
